﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project.WebAPI.Filters
{
    public class ApiResultFilterAttribute : Attribute, IActionFilter,IAuthorizationFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            //特殊处理：对有ApiIgnoreAttribute标签的，不进行返回结果包装，原样输出
            var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
            if (controllerActionDescriptor != null)
            {
                var isDefined = controllerActionDescriptor.EndpointMetadata.Any(a => a.GetType().Equals(typeof(ApiIgnoreAttribute)));
                if (isDefined)
                {
                    return;
                }

            }
            // 返回结果为JsonResult的请求进行Result包装
            if (context.Result != null)
            {
                if (context.Result is ObjectResult)
                {
                    var result = context.Result as ObjectResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, success=true, msg = "success", data = result.Value });

                }
               
                else if (context.Result is NoContentResult)
                {
                    var result = context.Result as NoContentResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, success = true, msg = "success", data = new { } });
                }
                else if (context.Result is ContentResult)
                {
                    var result = context.Result as ContentResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, success = true, msg = result.Content,data=new { } });
                }
                else if(context.Result is BadRequestObjectResult)
                {
                    var result = context.Result as BadRequestObjectResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, success = false, msg = result.Value });
                }
                else if (context.Result is NotFoundResult)
                {
                    var result = context.Result as NotFoundResult;
                    context.Result = new JsonResult(new { code = result.StatusCode, success = false, msg = "请求资源不存在！" });
                }
                //else
                //{
                //    throw new Exception($"未经处理的Result类型：{ context.Result.GetType().Name}");
                //}

            }
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
         
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
            if (context.HttpContext.User==null)
            {
                context.Result = new JsonResult(new { code = 401, success = false, msg = "请登录" });
            }
        }
    }
}
